<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/1
 * Time: 18:50
 */

namespace app\storage\controller;

use app\storage\model\ClientVersionModel;
use cmf\controller\AdminBaseController;
use think\Request;

class ClientPdaController extends AdminBaseController{

    private $clientVersionModel;

    public function __construct(ClientVersionModel $clientVersionModel,Request $request = null)
    {
        parent::__construct($request);
        $this->clientVersionModel = $clientVersionModel;
    }

    public function index()
    {
        return $this->fetch('index');
    }

    public function add()
    {
        return $this->fetch('add');
    }

    public function clientVersionInfo(){
        $result = $this->clientVersionModel->select();
        return json($result);
    }

    public function edit(){
        $where['id'] = input('clientVersionId');
        $clientVersionInfo = $this->clientVersionModel->where($where)->find();
        $this->assign('clientVersionInfo',$clientVersionInfo);
        return $this->fetch('edit');
    }

    public function editClientVersion()
    {
        $data = input();
        $this->clientVersionModel->where('id',$data['id'])->update($data);
        $this->redirect('ClientPda/index');
    }

    public function checkClientVersionName()
    {
        $name = input('name');
        $result = $this->clientVersionModel->where("binary name = '$name'")->find();
        return boolval($result);
    }

    public function checkUrl()
    {
        $url = input('url');
        $result = $this->clientVersionModel->where("binary url = '$url'")->find();
        return boolval($result);
    }

    public function deleteVersion()
    {
        $versionId = input('versionId');
        $result = $this->clientVersionModel->where('id',$versionId)->delete();
        if((bool)$result === true){
            ajax_response(true,'删除成功');
        }
        else{
            ajax_response(false,'删除失败');
        }
    }

    public function addFile()
    {
        return $this->fetch('addFile');
    }


    public function addVersion()
    {
        $data = input();
        $data['type'] = 1;
        $this->clientVersionModel->allowField(true)->save($data);
        $this->redirect('client_pda/index');
    }

    public function upload()
    {
        $file = request()->file("fileUrl");
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/clientVersionFile');
            if ($info) {
                $url = str_replace("\\", "/", $info->getSaveName());
                $url = '/uploads/clientVersionFile'. DS .$url;
                $this->assign('Url',$url);
                return $this->fetch('add');
            } else {
                $this->error($file->getError());
            }
        }
    }

}
